package com.diqye.templet.core;





/**
 * 字符串替换类
 *
 * @author 秦振龙
 * @date 2013-7-2
 *
 * <b>修改记录及说明：</b>
 * <ul>
 * <li>2013-7-2  秦振龙  创建</li>
 * </ul>
 */
public class ReplaceStr {
	
	private Method method;
	private Regullation regullation;
	
	
	
	public ReplaceStr(Method method, Regullation regullation) {
		super();
		this.method = method;
		this.regullation = regullation;
	}

	


	public ReplaceStr(Regullation regullation) {
		super();
		this.regullation = regullation;
	}




	/**
	 * 替换方法
	 * @param str 替换前字符串
	 * @return 替换后字符串
	 */
	public String replace(String str){
		return replace(str,this.method);
	}
	
	/**
	 * 替换方法
	 * @param str 替换前字符串
	 * @return 替换后字符串
	 */
	public String replace(String str, Method method){
		StringBuffer sbuf = new StringBuffer();
		int start = 0;
		int[] startRef = {start};
		int[] endRef = {0};
		
		while(regullation.invok(str, startRef, endRef)){
			sbuf.append(str.substring(start, startRef[0]));
			sbuf.append(method.invok(str.substring(startRef[0],endRef[0])));
			start = endRef[0];
			startRef[0] = start;
		}
		sbuf.append(str.substring(start));
		return sbuf.toString();
	}
	
	
	
	
}
